package com.loyal.loyalerp.utils;

import javax.annotation.PostConstruct;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loyal.loyalerp.basedata.permission.mapper.PermissionValidateMapper;
import com.loyal.loyalerp.model.GyUser;

@Service
public class ValidateUtil {

	@Autowired
	private PermissionValidateMapper permissionValidateMapper;

	public static ValidateUtil validateUtil;

	@PostConstruct
	public void beforeInit() {
		validateUtil = this;
	}

	public static boolean validateQuery(String operate) {
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_query";
		int i = 0;
		try {// 最好是从全局结构总读取判断
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return i != 0;
	}

	public static boolean validateEdit(String operate) {
		// 好一点的方法是从缓存中读取，登录的时候就把用户所有权限保存在全局的结构中
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_edit";
		int i = 0;
		try {
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
		}
		return i != 0;
	}

	public static boolean validateDel(String operate) {
		// 好一点的方法是从缓存中读取，登录的时候就把用户所有权限保存在全局的结构中
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_del";
		int i = 0;
		try {
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
		}
		return i != 0;
	}

	public static boolean validateAdd(String operate) {
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_add";
		int i = 0;
		try {
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
		}
		return i != 0;
	}

	public static boolean validateCheck(String operate) {
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_check";
		int i = 0;
		try {
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
		}
		return i != 0;
	}

	// 初始化
	public static boolean init(String operate) {
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_init";
		int i = 0;
		try {
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
		}
		return i != 0;
	}

	// 弃审
	public static boolean abandonCheck(String operate) {
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_abandonCheck";
		int i = 0;
		try {
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
		}
		return i != 0;
	}

	// 月结
	public static boolean closeMonth(String operate) {
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_closeMonth";
		int i = 0;
		try {
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
		}
		return i != 0;
	}

	public static boolean validateInput(String operate) {
		GyUser user = (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
		String code = operate + "_input";
		int i = 0;
		try {
			if (user.getId() == 1) {
				return true;
			}
			i = validateUtil.permissionValidateMapper.retrievePermissionCount(code, user.getId());
		} catch (Exception e) {
		}
		return i != 0;
	}

	public static GyUser validateUser() {
		return (GyUser) SecurityUtils.getSubject().getSession().getAttribute("user");
	}
}
